package com.example.demo.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.tools.DateTool;
import com.example.demo.sys.entity.Coursetype;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.demo.controller.Result;
import com.example.demo.sys.mapper.CoursecheckformMapper;
import com.example.demo.sys.entity.Coursecheckform;

import org.springframework.web.bind.annotation.RestController;
import com.example.demo.common.tools.BaseController;

/**
 * <p>
 *  前端控制器 chrimer
 * </p>
 *
 * @author chimer
 * @since 2020-06-22
 */
@RestController
@RequestMapping("/sys/coursecheckform")
public class CoursecheckformController extends BaseController {
    @Autowired
    private CoursecheckformMapper coursecheckformMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
            Integer pageNum,
            Integer pageSize,
            String courseName,
            String id) {
        Result result = new Result();
        Page<Coursecheckform> page = new Page<Coursecheckform>(pageNum, pageSize);
        Coursecheckform parms = new Coursecheckform();
        QueryWrapper<Coursecheckform> warpper = new QueryWrapper<Coursecheckform>(parms);
        if(courseName != null && !"".equals(courseName)){
            warpper.nested(i-> i.like("courseName",courseName).eq("userId",id));
        }
        else
            warpper.eq("userId",id);
        result.setData(coursecheckformMapper.selectPage(page, warpper));
        return result;
    }

    //教师课程管理只显示已经审核通过的课程
    @RequestMapping(method= RequestMethod.POST,value="/passpage")
    public Result passpage(
            Integer pageNum,
            Integer pageSize,
            String courseName,
            String id
    ) {
        Result result = new Result();
        Page<Coursecheckform> page = new Page<Coursecheckform>(pageNum, pageSize);
        Coursecheckform parms = new Coursecheckform();
        QueryWrapper<Coursecheckform> warpper = new QueryWrapper<Coursecheckform>(parms);

        if(courseName != null && !"".equals(courseName)){
            warpper.nested(i-> i.like("courseName",courseName).eq("userId",id));
        }

//        warpper.eq("status",1);

        else
            warpper.eq("userId",id);


        result.setData(coursecheckformMapper.selectPage(page, warpper));
        return result;
    }

    //课程详情页请求对应的数据
    @RequestMapping(method= RequestMethod.POST,value="/page123")
    public Result page123() {
        Result result = new Result();
        Page<Coursecheckform> page = new Page<Coursecheckform>();
        Coursecheckform parms = new Coursecheckform();
        QueryWrapper<Coursecheckform> warpper = new QueryWrapper<Coursecheckform>(parms);

        result.setData(coursecheckformMapper.selectPage(page, warpper));
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/applypage")//管理员审核课程
    public Result applypage(
            Integer pageNum,
            Integer pageSize,
            String courseName) {
        Result result = new Result();
        Page<Coursecheckform> page = new Page<Coursecheckform>(pageNum, pageSize);
        Coursecheckform parms = new Coursecheckform();
        QueryWrapper<Coursecheckform> warpper = new QueryWrapper<Coursecheckform>(parms);
        if(courseName != null && !"".equals(courseName)){
            warpper.nested(i-> i.like("courseName",courseName).eq("status",2));
        }
        else warpper.eq("status",2);
        result.setData(coursecheckformMapper.selectPage(page, warpper));
        return result;

    }

    @RequestMapping(method= RequestMethod.POST,value="/selectpage")//管理员查找课程
    public Result selectpage(
            Integer pageNum,
            Integer pageSize,
            String courseType,
            String courseName) {
        Result result = new Result();
        Page<Coursecheckform> page = new Page<Coursecheckform>(pageNum, pageSize);
        Coursecheckform parms = new Coursecheckform();
        QueryWrapper<Coursecheckform> warpper = new QueryWrapper<Coursecheckform>(parms);
        if((courseName != null && !"".equals(courseName))&&(courseType != null && !"".equals(courseType))){
            warpper.nested(i-> i.eq("courseType",courseType).like("courseName",courseName).eq("status",1));
        }
        else if(courseType != null && !"".equals(courseType)){
            warpper.nested(i-> i.eq("courseType",courseType).eq("status",1));
        }
        else if(courseName != null && !"".equals(courseName)){
            warpper.nested(i-> i.like("courseName",courseName).eq("status",1));
        }
        else warpper.eq("status",1);
        result.setData(coursecheckformMapper.selectPage(page, warpper));
        return result;

    }

    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody Coursecheckform entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            entity.setCreateTime(DateTool.getCurrTime());
            entity.setStatus(2);
            coursecheckformMapper.insert(entity);
        } else {
            coursecheckformMapper.updateById(entity);
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String id) {
        Result result = new Result();
//        List<String> deleteIds = new ArrayList<String>();
//        for (String id : ids.split(",")) {
//            deleteIds.add(id);
//        }
        coursecheckformMapper.DeleteCoursemananger(id);
        coursecheckformMapper.DeleteDiscuss(id);
        coursecheckformMapper.DeleteForum(id);
        coursecheckformMapper.DeleteStudentWork(id);
        coursecheckformMapper.DeleteVideo(id);
        coursecheckformMapper.DeleteWork(id);


        coursecheckformMapper.deleteById(id);
        return result;
    }
    //更新状态
    @RequestMapping(method= RequestMethod.POST,value="/updateUseful")
    public Result updateUseful(String ids, Integer status){
        Result result = new Result();
        String[] aryIds = ids.split(",");
        for(String id : aryIds){
            coursecheckformMapper.updateuseful(id,status);
        }

        return result;
    }

    //查看课程信息
    @RequestMapping(method= RequestMethod.POST,value="/selectclass")
    public Result selectclass(String id) {
        Result result = new Result();
        Coursecheckform courseinfor = new Coursecheckform();
        QueryWrapper<Coursecheckform> warpper = new QueryWrapper<Coursecheckform>(courseinfor);
        warpper.eq("id",id);
        result.setData(coursecheckformMapper.selectOne(warpper));
        return result;
    }

    //按分类遍历课程
    @RequestMapping(method= RequestMethod.POST,value="/selectclasstest")
    public Result selectclasstest() {
        Result result = new Result();
        Coursecheckform coursecheckform = new Coursecheckform();
        QueryWrapper<Coursecheckform> Bwarpper = new QueryWrapper<Coursecheckform>(coursecheckform);
        List<String> coursetype = coursecheckformMapper.SelectType();
        List<List<Coursecheckform>> courselist = new ArrayList<>();
        for(int i=0;i<coursetype.size();i++) {
            List<Coursecheckform> courselist1 = null;
            courselist1 = coursecheckformMapper.SelectT(coursetype.get(i),1);
            courselist.add(courselist1);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("courselist", courselist);
        map.put("coursetype",coursetype);
        result.setData(map);
        return result;
    }

    //查询课程
    @RequestMapping(method= RequestMethod.POST,value="/selecttclasstest")
    public Result selecttclasstest(String name) {
        Result result = new Result();
        Coursecheckform coursecheckform = new Coursecheckform();
        QueryWrapper<Coursecheckform> Bwarpper = new QueryWrapper<Coursecheckform>(coursecheckform);
        List<String> coursetype = coursecheckformMapper.SelectType();
        List<List<Coursecheckform>> courselist = new ArrayList<>();
        for(int i=0;i<coursetype.size();i++) {
            List<Coursecheckform> courselist1 = null;
            courselist1 = coursecheckformMapper.SelectTT(coursetype.get(i),name,1);
            courselist.add(courselist1);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("courselist", courselist);
        map.put("coursetype",coursetype);
        result.setData(map);
        return result;
    }

}
